package com.ApachenData;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.testng.annotations.Test;

import java.io.FileInputStream;


public class GetExcelData {
    @Test
    public void getExcelData() throws Exception {
        //创建io对象从磁盘读取文件
        FileInputStream fs = new FileInputStream("E:\\1.xls");

        //使用POI接收java IO从磁盘读取的整体excel文件
//       XSSFWorkbook workbook = new XSSFWorkbook(fs);
        HSSFWorkbook hf = new HSSFWorkbook(fs);
        //获取Sheet页
//        XSSFSheet sheet1 = workbook.getSheet("Sheet1");
        HSSFSheet sheet1 = hf.getSheet("Sheet1");

        //获取到总列数
        short lastCellNum = sheet1.getRow(0).getLastCellNum();
        System.out.print("总列数：" + lastCellNum + "\t");

        //获取总行数
        int lastRowNum = sheet1.getLastRowNum();
        System.out.println("总行数：" + lastRowNum);

        //存入二维数组  行 列  x y
        Object[][] excelData = new Object[lastRowNum][lastCellNum];

        //遍历行数 i<总行数
        for (int i = 1; i < lastRowNum; i++) {
            //获取到每行的数据
//            XSSFRow row = sheet1.getRow(i);
            HSSFRow row = sheet1.getRow(i);

            //两列 i<总列数
            for (int j = 0; j < lastCellNum; j++) {
                //获取每个单元格的数据
//                XSSFCell cell = row.getCell(j);
                HSSFCell cell = row.getCell(j);


//                System.out.println(cell.getStringCellValue());
                //存入二维数组
                excelData[i - 1][j] = cell.getStringCellValue();
                System.out.println(excelData[i - 1][j]);
            }
        }

    }
}
